﻿using System.Xml;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

namespace AutoRoot
{
   public class MyStory
   {
      public string name
      {
         get;
         set;
      }

      private LinkedList<Section> myList = new LinkedList<Section>();

      public MyStory()
      {
      }

      public void ReadFromXML(string xml)
      {
         /*
          * xml format
          * 
          * <story name="myStory">
          *    <section id="a" name="section a" resource="a's path" decisionResource="a's decision path">
          *       <section id="a" name="section aa" resource="aa's path" decisionResource="aa's decision path">
          *       </section>
          *    </section>
          * </story>
          * 
          */
         XmlTextReader reader = new  XmlTextReader(xml);
         reader.ReadToFollowing("story");
         reader.MoveToAttribute("name");
         name = reader.Value;

         while (reader.Read())
         {
            if (reader.ReadToFollowing("section"))
            {
               Section newSection = new Section();
               reader.MoveToAttribute("id");
               newSection.id = reader.Value;
               reader.MoveToAttribute("name");
               newSection.name = reader.Value;
               reader.MoveToAttribute("resource");
               newSection.resourcePath = Application.persistentDataPath + reader.Value;
               reader.MoveToAttribute("decisionResource");
               newSection.decisionResourcePath = Application.persistentDataPath + reader.Value;
               myList.AddLast(newSection);
            }
         }
      }

      public LinkedListNode<Section> GetFirst()
      {
         return myList.First;
      }

      public LinkedListNode<Section> GetLast()
      {
         return myList.Last;
      }

      public void Remove(LinkedListNode<Section> node)
      {
         myList.Remove(node);
      }

      public void PushBack(Section section)
      {
         myList.AddLast(section);
      }
   }
}
